<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>style.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_testing_ui_style.js.html">style.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2008 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Tools for testing Closure renderers against static markup
<a name="line17"></a> * spec pages.
<a name="line18"></a> *
<a name="line19"></a> */
<a name="line20"></a>
<a name="line21"></a>goog.provide(&#39;goog.testing.ui.style&#39;);
<a name="line22"></a>
<a name="line23"></a>goog.require(&#39;goog.array&#39;);
<a name="line24"></a>goog.require(&#39;goog.dom&#39;);
<a name="line25"></a>goog.require(&#39;goog.dom.classes&#39;);
<a name="line26"></a>goog.require(&#39;goog.testing.asserts&#39;);
<a name="line27"></a>
<a name="line28"></a>
<a name="line29"></a>/**
<a name="line30"></a> * Uses document.write to add an iFrame to the page with the reference path in
<a name="line31"></a> * the src attribute. Used for loading an html file containing reference
<a name="line32"></a> * structures to test against into the page. Should be called within the body of
<a name="line33"></a> * the jsunit test page.
<a name="line34"></a> * @param {string} referencePath A path to a reference HTML file.
<a name="line35"></a> */
<a name="line36"></a>goog.testing.ui.style.writeReferenceFrame = function(referencePath) {
<a name="line37"></a>  document.write(&#39;&lt;iframe id=&quot;reference&quot; name=&quot;reference&quot; &#39; +
<a name="line38"></a>      &#39;src=&quot;&#39; + referencePath + &#39;&quot;&gt;&lt;/iframe&gt;&#39;);
<a name="line39"></a>};
<a name="line40"></a>
<a name="line41"></a>
<a name="line42"></a>/**
<a name="line43"></a> * Returns a reference to the first element child of a node with the given id
<a name="line44"></a> * from the page loaded into the reference iFrame. Used to retrieve a particular
<a name="line45"></a> * reference DOM structure to test against.
<a name="line46"></a> * @param {string} referenceId The id of a container element for a reference
<a name="line47"></a> *   structure in the reference page.
<a name="line48"></a> * @return {Node} The root element of the reference structure.
<a name="line49"></a> */
<a name="line50"></a>goog.testing.ui.style.getReferenceNode = function(referenceId) {
<a name="line51"></a>  return goog.dom.getFirstElementChild(
<a name="line52"></a>      window.frames[&#39;reference&#39;].document.getElementById(referenceId));
<a name="line53"></a>};
<a name="line54"></a>
<a name="line55"></a>
<a name="line56"></a>/**
<a name="line57"></a> * Returns an array of all element children of a given node.
<a name="line58"></a> * @param {Node} element The node to get element children of.
<a name="line59"></a> * @return {Array.&lt;Node&gt;} An array of all the element children.
<a name="line60"></a> */
<a name="line61"></a>goog.testing.ui.style.getElementChildren = function(element) {
<a name="line62"></a>  var first = goog.dom.getFirstElementChild(element);
<a name="line63"></a>  if (!first) {
<a name="line64"></a>    return [];
<a name="line65"></a>  }
<a name="line66"></a>  var children = [first], next;
<a name="line67"></a>  while (next = goog.dom.getNextElementSibling(children[children.length - 1])) {
<a name="line68"></a>    children.push(next);
<a name="line69"></a>  }
<a name="line70"></a>  return children;
<a name="line71"></a>};
<a name="line72"></a>
<a name="line73"></a>
<a name="line74"></a>/**
<a name="line75"></a> * Tests whether a given node is a &quot;content&quot; node of a reference structure,
<a name="line76"></a> * which means it is allowed to have arbitrary children.
<a name="line77"></a> * @param {Node} element The node to test.
<a name="line78"></a> * @return {boolean} Whether the given node is a content node or not.
<a name="line79"></a> */
<a name="line80"></a>goog.testing.ui.style.isContentNode = function(element) {
<a name="line81"></a>  return element.className.indexOf(&#39;content&#39;) != -1;
<a name="line82"></a>};
<a name="line83"></a>
<a name="line84"></a>
<a name="line85"></a>/**
<a name="line86"></a> * Tests that the structure, node names, and classes of the given element are
<a name="line87"></a> * the same as the reference structure with the given id. Throws an error if the
<a name="line88"></a> * element doesn&#39;t have the same nodes at each level of the DOM with the same
<a name="line89"></a> * classes on each. The test ignores all DOM structure within content nodes.
<a name="line90"></a> * @param {Node} element The root node of the DOM structure to test.
<a name="line91"></a> * @param {string} referenceId The id of the container for the reference
<a name="line92"></a> *   structure to test against.
<a name="line93"></a> */
<a name="line94"></a>goog.testing.ui.style.assertStructureMatchesReference = function(element,
<a name="line95"></a>    referenceId) {
<a name="line96"></a>  goog.testing.ui.style.assertStructureMatchesReferenceInner_(element,
<a name="line97"></a>      goog.testing.ui.style.getReferenceNode(referenceId));
<a name="line98"></a>};
<a name="line99"></a>
<a name="line100"></a>
<a name="line101"></a>/**
<a name="line102"></a> * A recursive function for comparing structure, node names, and classes between
<a name="line103"></a> * a test and reference DOM structure. Throws an error if one of these things
<a name="line104"></a> * doesn&#39;t match. Used internally by
<a name="line105"></a> * {@link goog.testing.ui.style.assertStructureMatchesReference}.
<a name="line106"></a> * @param {Node} element DOM element to test.
<a name="line107"></a> * @param {Node} reference DOM element to use as a reference (test against).
<a name="line108"></a> * @private
<a name="line109"></a> */
<a name="line110"></a>goog.testing.ui.style.assertStructureMatchesReferenceInner_ = function(element,
<a name="line111"></a>    reference) {
<a name="line112"></a>  if (!element &amp;&amp; !reference) {
<a name="line113"></a>    return;
<a name="line114"></a>  }
<a name="line115"></a>  assertTrue(&#39;Expected two elements.&#39;, !!element &amp;&amp; !!reference);
<a name="line116"></a>  assertEquals(&#39;Expected nodes to have the same nodeName.&#39;,
<a name="line117"></a>      element.nodeName, reference.nodeName);
<a name="line118"></a>  var elementClasses = goog.dom.classes.get(element);
<a name="line119"></a>  goog.array.forEach(goog.dom.classes.get(reference), function(referenceClass) {
<a name="line120"></a>    assertContains(&#39;Expected test node to have all reference classes.&#39;,
<a name="line121"></a>        referenceClass, elementClasses);
<a name="line122"></a>  });
<a name="line123"></a>  // Call assertStructureMatchesReferenceInner_ on all element children
<a name="line124"></a>  // unless this is a content node
<a name="line125"></a>  var elChildren = goog.testing.ui.style.getElementChildren(element),
<a name="line126"></a>      refChildren = goog.testing.ui.style.getElementChildren(reference);
<a name="line127"></a>  if (!goog.testing.ui.style.isContentNode(reference)) {
<a name="line128"></a>    if (elChildren.length != refChildren.length) {
<a name="line129"></a>      assertEquals(&#39;Expected same number of children for a non-content node.&#39;,
<a name="line130"></a>          elChildren.length, refChildren.length);
<a name="line131"></a>    }
<a name="line132"></a>    for (var i = 0; i &lt; elChildren.length; i++) {
<a name="line133"></a>      goog.testing.ui.style.assertStructureMatchesReferenceInner_(elChildren[i],
<a name="line134"></a>          refChildren[i]);
<a name="line135"></a>    }
<a name="line136"></a>  }
<a name="line137"></a>};
</pre>


</body>
</html>
